import 'package:common_utils/common_utils.dart';
import 'package:fate_blind_box/config/event_bus.dart';
import 'package:fate_blind_box/config/event_keys.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';

import 'logic.dart';

class PersonHomePage extends StatefulWidget {
  const PersonHomePage({super.key});

  @override
  State<PersonHomePage> createState() => _PersonHomePageState();
}

final logic = Get.find<PersonHomeLogic>();
final state = Get.find<PersonHomeLogic>().state;

class _PersonHomePageState extends State<PersonHomePage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initUnreadData();
    initEventBus();
    logic.checkInvite();
    logic.hiddenPayLogic();
  }

  /// 获取消息未读数
  initUnreadData() async {
    logic.state.unreadCount.value =
        await EMClient.getInstance.chatManager.getUnreadMessageCount();
    LogUtil.e(
        tag: 'PersonHomePage', '消息未读数====${logic.state.unreadCount.value}');
    if (logic.state.unreadCount.value > 0) {
      logic.state.hasUnreadMessage.value = true;
    } else {
      logic.state.hasUnreadMessage.value = false;
    }
    setState(() {
      logic.initBottomTab();
    });
  }

  /// 监听角标
  initEventBus() {
    EventBus.instance.addListener(EventKeys.chatUpdateBadge, (arg) {
      LogUtil.e(tag: 'PersonHomeLogic', '收到角标更新$arg');
      state.unreadCount.value = arg;
      if (state.unreadCount.value > 0) {
        state.hasUnreadMessage.value = true;
      } else {
        state.hasUnreadMessage.value = false;
      }
      setState(() {
        logic.initBottomTab();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: PageView(
          controller: logic.state.pageController,
          onPageChanged: (index) {
            if (logic.state.currentIndex.value != index) {
              logic.state.currentIndex.value = index;
            }
          },
          physics: const NeverScrollableScrollPhysics(),
          children: logic.state.pageList, // 禁止滑动
        ),
        backgroundColor: Colors.white,
        bottomNavigationBar: Obx(() {
          return BottomNavigationBar(
            currentIndex: logic.state.currentIndex.value,
            backgroundColor: Colors.white,
            iconSize: 12.w,
            onTap: (index) {
              if (logic.state.currentIndex.value != index) {
                logic.state.currentIndex.value = index;
                logic.state.pageController?.jumpToPage(index);
                if (index == 1) {
                  logic.initRefresh();
                }
              }
            },
            type: BottomNavigationBarType.fixed,
            // fixedColor: ThemeColor.colorD88E4B,
            selectedItemColor: ThemeColor.colorD88E4B,
            items: logic.bottomTab,
          );
        }));
  }
}

class a extends InheritedWidget {
  a({super.key, required super.child});

  @override
  bool updateShouldNotify(covariant InheritedWidget oldWidget) {
    // TODO: implement updateShouldNotify
    return this != oldWidget;
  }
}
